﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml.Serialization;
using System.IO;


/*
 * 
 * 首先创建2张表
 * 
 * CREATE TABLE Log_Common
(LogId INT PRIMARY KEY NOT NULL, AccountName VARCHAR(50) NOT NULL,[Message] NVARCHAR(4000) ,[Object] VARCHAR(MAX), IPAddress VARCHAR(255), LogTime DATETIME DEFAULT(GETDATE()), LogType INT)
 * 
 * 
 * 
 * 
 * 
 * CREATE TABLE [dbo].[Log_Login](
	[Logid] [int] IDENTITY(1,1) NOT NULL,
	[AccountName] [varchar](36) NOT NULL,
	[Message] [varchar](max) NOT NULL,
	[Exception] [varchar](400) NULL,
	[LogTime] [datetime] NOT NULL CONSTRAINT [DF_Log_Login_LogTime]  DEFAULT (getdate()),
 CONSTRAINT [PK__Log_Login__0E04126B] PRIMARY KEY CLUSTERED 
(
	[Logid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 * 
 * 
 * 使用方式
 * int testint = 100;
 * Log_Common log = new Log_Common()
 * {AccountName = "Zeus2", Message = "测试", Object =  GetStringFromObject(testint), LogType = 1};
    Log_Common.AddLog(log);
 * 
 * 
 * Log_Login loginlog = new Log_Login();
 * loglog对象赋值
 * 
 * Log_Login.InsertLog(loginlog);
 * 
 */
/// <summary>
/// 日志记录公共类
/// </summary>
[Serializable()]
[HZ.Table(TableName = "Log_Common")]
public class Log_Common
{
    /// <summary>
    /// 日志编号
    /// </summary>
    [HZ.Column(PrimaryKey = true)]
    public int? Logid { get; set; }
    /// <summary>
    /// 用户名
    /// </summary>
    public string AccountName { get; set; }
    /// <summary>
    /// 信息
    /// </summary>
    public string Message { get; set; }

    /// <summary>
    /// 待记录的实体对象，如删除前和修改前的对象
    /// Log_Common.GetStringFromObject(object);
    /// </summary>
    public string Object { get; set; }
    /// <summary>
    /// 记录IP地址
    /// </summary>
    public string IPAddress { get; set; }
    /// <summary>
    /// 记录时间无须赋值
    /// </summary>
    public DateTime? LogTime { get; set; }

    /// <summary>
    /// 日志类型
    /// 1、正常操作提醒
    /// 2、审核企业
    /// 3、审核商品
    /// 
    /// 4、企业修改企业信息
    /// 5、企业修改商品信息
    /// 
    /// 10、其他类型
    /// </summary>
    public int? LogType { get; set; }

    /// <summary>
    /// 增加日志
    /// </summary>
    /// <param name="log"></param>
    /// <returns></returns>
    public static int AddLog(Log_Common log)
    {
        log.Logid = null;
        return HZ.Data.DAL_Helper.OperateTableSQL(log, HZ.Data.TableOperator.Insert);
    }

    /// <summary>
    /// 将对象序列化为字符串保存
    /// </summary>
    /// <param name="oo">待序列化的对象</param>
    /// <returns></returns>
    public static string GetStringFromObject(object oo)
    {
        if (oo is DataRow)
        {
            DataRow dr = oo as DataRow;
            oo = (oo as DataRow).Table;
            ((DataTable)oo).TableName = "DefaultName";
            ((DataTable)oo).Rows.Add(dr);
        }

        if (oo is DataTable)
        {
            ((DataTable)oo).TableName = "DefaultName";
        }
        XmlSerializer ser = new XmlSerializer(oo.GetType());
        var fs = new MemoryStream();
        ser.Serialize(fs, oo);
        fs.Close();

        return Encoding.UTF8.GetString(fs.ToArray());
    }

}

/// <summary>
/// 登录日志实体类
/// </summary>
[Serializable()]
[HZ.Table(TableName = "Log_Login")]
public class Log_Login
{
    /// <summary>
    /// 日志编号
    /// </summary>
    [HZ.Column(PrimaryKey = true)]
    public int? Logid { get; set; }
    /// <summary>
    /// 用户名
    /// </summary>
    public string AccountName { get; set; }
    /// <summary>
    /// 信息
    /// </summary>
    public string Message { get; set; }
    /// <summary>
    /// 异常
    /// </summary>
    public string Exception { get; set; }

    /// <summary>
    /// 记录时间无须赋值
    /// </summary>
    public DateTime? LogTime { get; set; }

    public static int InsertLog(Log_Login loginlog)
    {
        loginlog.Logid = null;
        return HZ.Data.DAL_Helper.OperateTableSQL(loginlog, HZ.Data.TableOperator.Insert);
    }
}